﻿using System;
using System.IO;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace PickGold
{
	/// <summary>
	/// StringBuilder的字符串处理类
	/// </summary>
	public class TextBuilder
	{
		#region 构造函数
		public TextBuilder()
			: this(0)
		{

		}

		public TextBuilder(int initialCapacity)
		{
			if (initialCapacity < TextBuilder.PaddingSize)
			{
				initialCapacity = TextBuilder.PaddingSize;
			}
			this.EnsureCapacityInternalBegin(initialCapacity);
		}

		public TextBuilder(string s)
			: this(s.ToCharArray(), 0, s.Length)
		{

		}

		public TextBuilder(char[] chars)
			: this(chars, 0, chars.Length)
		{

		}

		private TextBuilder(char[] chars, int startIndex, int length)
		{
			if (length != 0)
			{
				this.EnsureCapacityInternalBegin(length);
				TextBuilder.UnsafeCopyChars(this._data, 0, chars, startIndex, length);
				this._length = length;
			}
		}

		#region 构造函数专用调用函数
		/// <summary>
		/// 构造函数专用调用函数
		/// </summary>
		/// <param name="capacity"></param>
		private void EnsureCapacityInternalBegin(int capacity)
		{
			if (this._length < capacity)//真实长度 和构造长度的比较
			{
				if (capacity > TextBuilder.PaddingSize)
				{
					capacity += TextBuilder.PaddingSize * 2;
				}
				else
				{
					capacity = TextBuilder.PaddingSize;
				}

				char[] chArray1 = new char[capacity];
				this._data = chArray1;
				this._capacity = capacity;
			}
		}
		#endregion

		#endregion

		#region 添加字符
		public TextBuilder Append(string s)
		{
			return this.Append(s.ToCharArray(), 0, s.Length);
		}

		public TextBuilder Append(char[] chars)
		{
			return this.Append(chars, 0, chars.Length);
		}

		public TextBuilder Append(char[] chars, int length)
		{
			return this.Append(chars, 0, length);
		}

		public TextBuilder Append(string s, int length)
		{
			return this.Append(s.ToCharArray(0, length), 0, length);
		}

		public TextBuilder Append(string s, int startIndex, int length)
		{
			return this.Append(s.ToCharArray(startIndex, length), 0, length);
		}

		#region 最终的字符添加函数
		/// <summary>
		/// 最终的字符添加函数
		/// </summary>
		/// <param name="chars">新加的字符数组</param>
		/// <param name="startIndex">开始位置</param>
		/// <param name="length">长度</param>
		/// <returns>返回类型</returns>
		public TextBuilder Append(char[] chars, int startIndex, int length)
		{
			if (length < 1) return this;

			if (chars == null)
			{
				throw new ArgumentNullException("chars");//写上参数名就可以了
			}

			if ((startIndex < 0) || (chars.Length < (startIndex + length)))
			{
				throw new ArgumentOutOfRangeException();
			}

			this.EnsureCapacityInternal(this._length + length);//真实长度加上这次要附加的长度
			TextBuilder.UnsafeAndChars(this._data, chars, this._length, startIndex, length);
			this._length += length;//重新计算真实使用长度
			this._cachedString = null;//清楚缓存字符串
			return this;
		}
		#endregion

		#endregion

		#region 清空字符
		public void Clear()
		{
			this._length = 0;
			this._data = new char[0];
			this._cachedString = null;
		}
		#endregion

		#region 返回字符

		public char[] ToCharArray()
		{
			return this.ToCharArrayInternal(0, this._length);
		}

		public char[] ToCharArray(int startIndex, int length)
		{
			if ((startIndex < 0) || (this._length < (startIndex + length)))
			{
				throw new ArgumentOutOfRangeException();
			}
			return this.ToCharArrayInternal(startIndex, length);
		}

		private char[] ToCharArrayInternal(int startIndex, int length)
		{
			if (length == 0)
			{
				return new char[0];
			}

			char[] chArray1 = new char[length];
			TextBuilder.UnsafeCopyChars(chArray1, 0, this._data, startIndex, length);
			return chArray1;
		}

		#endregion

		#region 查找字符
		public int IndexOf(char ch)
		{
			if (this._length != 0)
			{
				return TextBuilder.UnsafeIndexOf(this._data, 0, this._length, ch);
			}
			return -1;
		}

		public int IndexOf(string s)
		{
			return this.IndexOf(s, 0, false);
		}

		public int IndexOf(string s, int startIndex)
		{
			if (startIndex < 0 || startIndex > this._length)
			{
				throw new ArgumentOutOfRangeException("startIndex");
			}
			return this.IndexOf(s, startIndex, false);
		}

		public int IndexOf(string s, bool ignoreCase)
		{
			return this.IndexOf(s, 0, ignoreCase);
		}

		public int IndexOf(string s, int startIndex, bool ignoreCase)
		{
			if (s == null)
			{
				throw new ArgumentNullException("s");
			}

			int num1 = s.Length;
			if (num1 == 0)
			{
				return -1;//要是要查找的字符串为空，就返回-1
			}

			if (this._length >= num1)//目前数组内长度大于要查找字符的长度
			{
				int num2 = this._length - num1;//找到可能的最后的启示位置。在忘后，不可能超过这个位置。
				if (startIndex <= num2)//开始位置不可以大于头最终结束位置
				{
					if (!ignoreCase)//区分大小写
					{
						return TextBuilder.CompareChars(this._data, s, startIndex, num2, num1);
					}
					else//不区分大小写
					{
						return TextBuilder.CompareCharsIgnoreCase(this._data, s, startIndex, num2, num1);
					}
				}
			}
			return -1;
		}
		#endregion

		#region 替换字符

		#region 替换字符串
		/// <summary>
		/// 替换字符串
		/// </summary>
		/// <param name="find">要查找的字符串</param>
		/// <param name="replacement">要替换的字符串</param>
		public void Replace(string find, string replacement)
		{
			this.Replace(find, replacement, 0);
		}
		#endregion

		#region 替换字符串
		/// <summary>
		/// 替换字符串
		/// </summary>
		/// <param name="find">要查找的字符串</param>
		/// <param name="replacement">要替换的字符串</param>
		/// <param name="startIndex">起始位置</param>
		public void Replace(string find, string replacement, int startIndex)
		{
			this.Replace(find, replacement, startIndex, false, 0);
		}
		#endregion

		#region 替换字符串
		/// <summary>
		/// 替换字符串
		/// </summary>
		/// <param name="find">要查找的字符串</param>
		/// <param name="replacement">要替换的字符串</param>
		/// <param name="ignoreCase">是否不区分大小字</param>
		public void Replace(string find, string replacement, bool ignoreCase)
		{
			this.Replace(find, replacement, 0, ignoreCase, 0);
		}
		#endregion

		#region 替换字符串
		/// <summary>
		/// 替换字符串
		/// </summary>
		/// <param name="find">要查找的字符串</param>
		/// <param name="replacement">要替换的字符串</param>
		/// <param name="startIndex">起始位置</param>
		/// <param name="ignoreCase">是否不区分大小字</param>
		public void Replace(string find, string replacement, int startIndex, bool ignoreCase)
		{
			this.Replace(find, replacement, startIndex, ignoreCase, 0);
		}
		#endregion

		#region 替换字符串最终调用函数
		/// <summary>
		/// 替换字符串最终调用函数
		/// </summary>
		/// <param name="find">要查找的字符串</param>
		/// <param name="replacement">要替换的字符串</param>
		/// <param name="startIndex">起始位置</param>
		/// <param name="ignoreCase">是否不区分大小字</param>
		/// <param name="replaceCount">替换字数.如果小于1则替换全部</param>
		public void Replace(string find, string replacement, int startIndex, bool ignoreCase, int replaceCount)
		{
			if (find == null || replacement == null)
			{
				throw new ArgumentNullException();
			}

			if (find.Length > this._length) return;

			int bufferlen = replacement.Length - find.Length;//知道要替换的数量比目前数量多多少

			if ((bufferlen > 0) && (replaceCount >= 1))
			{
				this.EnsureCapacityInternal(this._length + replaceCount * bufferlen);//申请内部数组位置
			}

			//=====================================================//

			int c = 0;//这个是替换次数，记数变量
			int index = 0, sourceIndex = 0;
			int FindLenght = find.Length;
			int ReplaceLenght = replacement.Length;
			char[] ReplaceArray = replacement.ToCharArray();
			while (true)
			{
				//找到第一个启示位置
				index = this.IndexOf(find, startIndex, ignoreCase);

				if (index >= 0)//找到了
				{
					System.Buffer.BlockCopy(this._data, (index + FindLenght) * 2, this._data, (index + ReplaceLenght) * 2, (this._length - (index + FindLenght)) * 2);
					if (ReplaceLenght > 0)
					{
						System.Buffer.BlockCopy(ReplaceArray, 0, this._data, index * 2, ReplaceLenght * 2);
					}
					//移动指针
					sourceIndex = index + ReplaceLenght;
					startIndex = sourceIndex;
					this._length += bufferlen;
					c++;
					if (startIndex > this._length || (replaceCount > 0 && c >= replaceCount)) break;
				}
				else
				{
					break;
				}
			}//end while

			if (c > 0)
			{
				this._cachedString = null;
			}
		}
		#endregion

		#endregion

		//---------------------------------------------//

		#region 从文件载入数据

		#region 从文本文件载入
		/// <summary>
		/// 从文本文件载入
		/// </summary>
		public static TextBuilder FromFile(string fileName)
		{
			return TextBuilder.FromFile(fileName, System.Text.Encoding.Default);
		}
		#endregion

		#region 从文本文件载入
		/// <summary>
		/// 从文本文件载入
		/// </summary>
		/// <param name="fileName">文件名</param>
		/// <param name="charset">指定的编码类型</param>
		/// <returns></returns>
		public static TextBuilder FromFile(string fileName, System.Text.Encoding charset)
		{
			TextBuilder text = null;
			StreamReader stream = null;
			try
			{
				stream = new StreamReader(fileName, charset);
				text = new TextBuilder(stream.ReadToEnd());
			}
			catch
			{
				text = null;
			}
			finally
			{
				if (stream != null) stream.Close();
			}
			return text;
		}
		#endregion

		#endregion

		#region 删除字符

		#region 重新指定数组内真实长度，效果等同与删除指定位置后的数据
		/// <summary>
		/// 重新指定数组内真实长度，效果等同与删除指定位置后的数据
		/// </summary>
		/// <param name="startIndex"></param>
		/// <returns>bool型变量</returns>
		public bool Delete(int startIndex)
		{
			if ((startIndex < 0) || (this._data == null) || (this._capacity < startIndex) || (this._length < startIndex))
			{
				return false;
			}
			this._length = startIndex;//更改当前数组内使用真实长度变量数值
			this._cachedString = null;
			return true;
		}
		#endregion

		#region 从指定位置，向后删除指定长度的字符
		/// <summary>
		/// 从指定位置，向后删除指定长度的字符
		/// </summary>
		/// <param name="startIndex">开始位置</param>
		/// <param name="length">删除的长度</param>
		/// <returns>bool型变量</returns>
		public bool Delete(int startIndex, int length)
		{
			if (((startIndex < 0) || (this._data == null)) || (startIndex > this.Length) || ((length + startIndex) > this._length))
			{
				return false;
			}
			System.Buffer.BlockCopy(this._data, (startIndex + length) * 2, this._data, startIndex * 2, (this.Length - (length + startIndex)) * 2);
			this._length -= length;
			this._cachedString = null;
			return true;
		}
		#endregion

		#endregion

		#region 插入字符

		public void Insert(int index, char c)
		{
			if ((index < 0) || (index > this._length))
			{
				throw new ArgumentOutOfRangeException("index");
			}

			this.EnsureCapacityInternal(this._length + 1);//检测剩余位置，如果不足就申请位置
			TextBuilder.ShiftCharsRight(this._data, index, this._length, 1);//插入位置到当前真实长度的所有元素后移动一位，空出一个位置放要插入的字符
			this._data[index] = c;
			this._length++;
			this._cachedString = null;
		}

		public void Insert(int index, char[] chars)
		{
			if (chars == null)
			{
				throw new ArgumentNullException("chars");
			}
			this.Insert(index, chars, 0, chars.Length);
		}

		public void Insert(int index, string s)
		{
			this.Insert(index, s.ToCharArray());
		}

		public void Insert(int index, char[] chars, int start, int length)
		{
			if (length < 1)
			{
				throw new ArgumentNullException("length");
			}

			if (start < 0)
			{
				throw new ArgumentNullException("start");
			}

			if ((chars == null) || ((start + length) > chars.Length))
			{
				throw new ArgumentNullException("chars");
			}

			if ((index < 0) || (index > this._length))
			{
				throw new ArgumentOutOfRangeException("index");
			}

			this.EnsureCapacityInternal(this._length + length);//判断并申请指定的长度
			TextBuilder.ShiftCharsRight(this._data, index, this._length, length);//当前数组在指定插入位置后的所有数据后移动length位。空出位置
			System.Buffer.BlockCopy(chars, start * 2, this._data, index * 2, length * 2);//在空出的位置上，把要插入的数据复制近来
			this._length += length;
			this._cachedString = null;
		}

		#endregion

		#region 判断是否以某字符串开头
		/// <summary>
		/// 判断是否以某字符串开头
		/// </summary>
		/// <param name="equalString">要判断的字符串</param>
		/// <returns>比较结果</returns>
		public bool StartWith(string equalString)
		{
			return this.StartWith(equalString, false);
		}

		/// <summary>
		/// 判断是否以某字符串开头
		/// </summary>
		/// <param name="equalString">要判断的字符串</param>
		/// <param name="ignoreCase">是否不区分大小写，True不区分，False区分</param>
		/// <returns>比较结果</returns>
		public bool StartWith(string equalString, bool ignoreCase)
		{
			return this.InWith(0, equalString, ignoreCase);
		}
		#endregion

		#region 判断是否以某字符串结尾
		/// <summary>
		/// 判断是否以某字符串结尾
		/// </summary>
		/// <param name="equalString">要判断的字符串</param>
		/// <returns>比较结果</returns>
		public bool EndWith(string equalString)
		{
			return this.EndWith(equalString, false);
		}

		/// <summary>
		/// 判断是否以某字符串结尾
		/// </summary>
		/// <param name="equalString">要判断的字符串</param>
		/// <param name="ignoreCase">是否不区分大小写，True不区分，False区分</param>
		/// <returns>比较结果</returns>
		public bool EndWith(string equalString, bool ignoreCase)
		{
			if (equalString == null || equalString.Length < 1) return false;
			if (equalString.Length > this._length) return false;

			return this.InWith((this._length - equalString.Length), equalString, ignoreCase);
		}
		#endregion

		#region 判断字符是否相等
		/// <summary>
		/// 判断字符是否相同并区分大小写
		/// </summary>
		/// <param name="startIndex">起始索引位置</param>
		/// <param name="equalString">要判断的字符</param>
		/// <returns></returns>
		public bool InWith(int startIndex, string equalString)
		{
			return this.InWith(startIndex, equalString, false);
		}

		/// <summary>
		/// 判断字符是否相同
		/// </summary>
		/// <param name="startIndex">起始索引位置</param>
		/// <param name="equalString">要判断的字符</param>
		/// <param name="ignoreCase">是否不区分大小写</param>
		/// <returns></returns>
		public bool InWith(int startIndex, string equalString, bool ignoreCase)
		{
			int num1 = equalString.Length;
			if (equalString == null || num1 < 1) return false;
			if (startIndex < 0) return false;
			if ((startIndex + num1) > this._length) return false;//判断了最后垒加位置小于this._length。下面两个函数的第三个参数就可以使用一个小一些的变量了。

			if (this._length > num1)
			{
				int num2 = this._length - num1;
				if (startIndex <= num2)
				{
					if (!ignoreCase)//区分大小写
					{
						return TextBuilder.CompareChars(this._data, equalString, startIndex, num2, num1) != -1 ? true : false;
					}
					else//不区分大小写
					{
						return TextBuilder.CompareCharsIgnoreCase(this._data, equalString, startIndex, num2, num1) != -1 ? true : false;
					}
				}
			}
			return false;
		}
		#endregion

		#region 获取某部分字符

		public string SubString(int startIndex, int length)
		{
			if (startIndex < 0 || length < 1) return string.Empty;
			length = Math.Min(length, this._length - startIndex);//取得最小数值，防止超出内部数组最大长度多取
			return new string(this.ToCharArray(startIndex, length));
		}

		public string SubStr(int startIndex, int endIndex)
		{
			if (startIndex < 0 || endIndex < startIndex) return string.Empty;
			return SubString(startIndex, (endIndex - startIndex) + 1);
		}

		#endregion

		#region 从某字符串里拷贝

		#region 拷贝一个字符到类内，丢弃现在内部数组的全部数据
		/// <summary>
		/// 拷贝一个字符到类内，丢弃现在内部数组的全部数据
		/// </summary>
		/// <param name="ch">复制近来的字符</param>
		public void Copy(char ch)
		{
			this.EnsureCapacityInternal(1);
			this._data[0] = ch;
			this._length = 1;
			this._cachedString = null;
		}
		#endregion

		public void Copy(string s)
		{
			this.Copy(s.ToCharArray(), 0, s.Length);
		}

		public void Copy(char[] chars)
		{
			this.Copy(chars, 0, chars.Length);
		}

		public void Copy(string s, int length)
		{
			this.Copy(s.ToCharArray(0, length), 0, length);
		}

		public void Copy(char[] chars, int length)
		{
			this.Copy(chars, 0, length);
		}

		public void Copy(string s, int startIndex, int length)
		{
			this.Copy(s.ToCharArray(startIndex, length), 0, length);
		}

		#region 把某个字符数组拷贝到内部数组上，丢弃内部数组以前的全部数据
		/// <summary>
		/// 把某个字符数组拷贝到内部数组上，丢弃内部数组以前的全部数据
		/// </summary>
		/// <param name="chars">要拷贝的数组</param>
		/// <param name="startIndex">拷贝的开始位置</param>
		/// <param name="length">要拷贝的长度</param>
		public void Copy(char[] chars, int startIndex, int length)
		{
			if (length < 1) return;

			if (chars == null)
			{
				throw new ArgumentNullException("chars");
			}

			if ((startIndex < 0) || (chars.Length < (startIndex + length)))
			{
				throw new ArgumentOutOfRangeException();
			}

			this.EnsureCapacityInternal(length);//申请内部数组位置
			TextBuilder.UnsafeCopyChars(this._data, 0, chars, startIndex, length);
			this._cachedString = null;
			this._length = length;
		}
		#endregion

		#endregion

		/**********************************************/

		#region 重新设置数组容量函数
		/// <summary>
		/// 重新设置数组容量函数
		/// </summary>
		/// <param name="capacity">真实长度和新附加长度的总和</param>
		private void EnsureCapacityInternal(int capacity)
		{
			if ((this._capacity - this._length) < capacity)//目前数组剩余长度和最新请求的长度的对比
			{
				capacity += this._capacity * 2;
				char[] chArray1 = new char[capacity];
				if (this._length != 0)//保证目前类内保留的数据不是空的时候开始复制
				{
					TextBuilder.UnsafeCopyChars(chArray1, 0, this._data, 0, this._length);
				}
				this._data = chArray1;//改写保留数组
				this._capacity = capacity;//改写目前数组长度
			}
		}

		#endregion

		#region 使用指针复制数组函数
		/// <summary>
		/// 使用指针复制数组函数
		/// </summary>
		/// <param name="destChars">原先的数据数组</param>
		/// <param name="destStartIndex">原先的数组开始位置</param>
		/// <param name="srcChars">要新附加的数组</param>
		/// <param name="srcStartIndex">新附加数组开始位置</param>
		/// <param name="length">新附加的数组的长度</param>
		private static unsafe void UnsafeCopyChars(char[] destChars, int destStartIndex, char[] srcChars, int srcStartIndex, int length)
		{
			Buffer.BlockCopy(srcChars, srcStartIndex * 2, destChars, destStartIndex * 2, length * 2);
		}
		#endregion

		#region 直接用指针加字符函数
		/// <summary>
		/// 直接用指针加字符函数
		/// </summary>
		/// <param name="Sorce">原始的char数组</param>
		/// <param name="NewAdd">要添加的char数组</param>
		/// <param name="Length">原始的长度</param>
		/// <param name="startIndex">要添加数组的开始位置</param>
		/// <param name="AddLength">要添加数组的添加长度</param>
		private static unsafe void UnsafeAndChars(char[] Sorce, char[] NewAdd, int Length, int startIndex, int AddLength)
		{
			fixed (char* chRef1 = Sorce, chRef2 = NewAdd)
			{
				char* chPtr1 = chRef1 + Length;
				char* chPtr2 = chRef2 + startIndex;
				for (int num6 = startIndex; num6 < AddLength; num6++)
				{
					chPtr1[num6] = chPtr2[num6];
				}
			}
		}
		#endregion

		#region 返回字符串
		/// <summary>
		/// 返回字符串
		/// </summary>
		/// <returns></returns>
		public override string ToString()
		{
			if (this._cachedString == null)
			{
				if (this._length != 0)
				{
					this._cachedString = new string(this._data, 0, this._length);
				}
				else
				{
					this._cachedString = string.Empty;
				}
			}
			return this._cachedString;
		}
		#endregion

		#region 在字符数组内查找指定字符的函数
		/// <summary>
		/// 在字符数组内查找指定字符的函数
		/// </summary>
		/// <param name="chars">要查找的字符数组</param>
		/// <param name="startIndex">开始查找位置</param>
		/// <param name="length">要求查找的长度</param>
		/// <param name="ch">要查找的字符</param>
		/// <returns>返回要查找字符的第一个位置</returns>
		private static unsafe int UnsafeIndexOf(char[] chars, int startIndex, int length, char ch)
		{
			fixed (char* chRef1 = chars)
			{
				char* chPtr1 = chRef1 + startIndex;
				int num1 = startIndex;
				while (num1 < length)
				{
					if (chPtr1[0] == ch)
					{
						return num1;
					}
					num1++;
					chPtr1++;
				}
			}
			return -1;
		}
		#endregion

		#region 比较一个字符串是否出现在内部数组中
		/// <summary>
		/// 比较一个字符串是否出现在内部数组中
		/// </summary>
		/// <param name="Sorce">内部数组</param>
		/// <param name="s">要比较的字符串</param>
		/// <param name="startIndex">比较的开始位置</param>
		/// <param name="endIndex">比较的结束位置</param>
		/// <param name="mLength">要比较的字符串长度</param>
		/// <returns>位置索引</returns>
		private static unsafe int CompareChars(char[] Sorce, string s, int startIndex, int endIndex, int mLength)
		{
			char Bc = s[0];//开始字符
			char Ec = s[mLength - 1];//结束字符
			char[] chArray1 = s.ToCharArray();

			fixed (char* chRef1 = Sorce, chRef2 = chArray1)
			{
				char* chPtr1 = chRef1 + startIndex;
				char* chPtr2 = chRef2 + 0;
				char* chPtr3 = chRef1 + startIndex + mLength - 1;

				for (int num6 = startIndex; num6 < endIndex; num6++)
				{
					if ((chPtr1[0] == Bc) && (chPtr3[0] == Ec))
					{
						int endstart = mLength - 1;
						for (int i = 0; i < mLength; i++)
						{
							if (chPtr1[i] != chPtr2[i])
							{
								break;
							}
							if (i == endstart)
							{
								return num6 - startIndex;
							}
						}
					}
					chPtr1++;
					chPtr3++;
				}
			}
			return -1;
		}
		#endregion

		#region 比较一个字符串是否出现在内部数组中，不区分大小写
		/// <summary>
		/// 比较一个字符串是否出现在内部数组中，不区分大小写
		/// </summary>
		/// <param name="Sorce">内部数组</param>
		/// <param name="s">要比较的字符串</param>
		/// <param name="startIndex">比较的开始位置</param>
		/// <param name="endIndex">比较的结束位置</param>
		/// <param name="mLength">要比较的字符串长度</param>
		/// <returns>位置索引</returns>
		private static unsafe int CompareCharsIgnoreCase(char[] Sorce, string s, int startIndex, int endIndex, int mLength)
		{
			char Bc = char.ToUpper(s[0]);//开始字符
			char Ec = char.ToUpper(s[mLength - 1]);//结束字符
			char[] chArray1 = s.ToUpper().ToCharArray();

			fixed (char* chRef1 = Sorce, chRef2 = chArray1)
			{
				char* chPtr1 = chRef1 + startIndex;
				char* chPtr2 = chRef2 + 0;
				char* chPtr3 = chRef1 + startIndex + mLength - 1;

				for (int num6 = startIndex; num6 <= endIndex; num6++)
				{
					if ((char.ToUpper(chPtr1[0]) == Bc) && (char.ToUpper(chPtr3[0]) == Ec))
					{
						int endstart = mLength - 1;
						for (int i = 0; i < mLength; i++)
						{
							if (char.ToUpper(chPtr1[i]) != chPtr2[i])
							{
								break;
							}
							if (i == endstart)
							{
								return num6 - startIndex;
							}
						}
					}
					chPtr1++;
					chPtr3++;
				}
			}
			return -1;
		}
		#endregion

		#region 后移指定位置后的全部字符在某个长度位置后
		/// <summary>
		/// 后移指定位置后的全部字符在某个长度位置后
		/// </summary>
		/// <param name="chars">要移动和被移动的统一数组</param>
		/// <param name="startIndex">要开始移动的位置</param>
		/// <param name="endIndex">数组的长度</param>
		/// <param name="shiftAmount">移动的长度</param>
		private static void ShiftCharsRight(char[] chars, int startIndex, int endIndex, int shiftAmount)
		{
			System.Buffer.BlockCopy(chars, startIndex * 2, chars, (startIndex + shiftAmount) * 2, (endIndex - startIndex) * 2);
		}
		#endregion

		/**********************************************/

		#region 一些字段

		private string _cachedString = string.Empty;//缓存的字符串

		private char[] _data = null;//内部保留数组

		private int _length = 0;//数组内真实长度保留变量

		private int _capacity = 0;//数组长度初始化变量

		private const int PaddingSize = 20000;//类内默认的大小

		#endregion

		#region 一些公共属性

		#region 返回是否为空的属性
		/// <summary>
		/// 返回是否为空的属性
		/// </summary>
		public bool IsEmpty
		{
			get
			{
				return (this._length == 0);
			}
		}
		#endregion

		#region 字符索引器属性
		/// <summary>
		/// 字符索引器属性
		/// </summary>
		public char this[int index]
		{
			get
			{
				if ((index < 0) || (index >= this._length))
				{
					throw new ArgumentOutOfRangeException();
				}
				return this._data[index];
			}
			set
			{
				if ((index < 0) || (index >= this._length))
				{
					throw new ArgumentOutOfRangeException();
				}
				this._data[index] = value;
				this._cachedString = null;
			}
		}
		#endregion

		#region 返回目前内部数组大小的属性
		/// <summary>
		/// 返回目前内部数组大小的属性
		/// </summary>
		public int Capacity
		{
			get
			{
				return this._capacity;
			}
		}
		#endregion

		#region 返回当前存储字符的长度
		/// <summary>
		/// 返回当前存储字符的长度
		/// </summary>
		public int Length
		{
			get
			{
				return this._length;
			}
		}
		#endregion

		#endregion
	}
}
